package com.hzxy.yiyid.web.module.service.impl;

import com.hzxy.yiyid.web.module.controller.vo.PenaltyInfoRequestVO;
import com.hzxy.yiyid.web.module.service.IdentifyDoctorService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.*;

/**
 * @ClassName IdentifyDoctorServiceImpl
 * @Description 慧眼识医服务层
 * @Author RAICOM2023
 * @Date 2023/3/6 21:59
 * @Version 1.0
 **/
@Service(value = "identifyDoctorService")
@Transactional
public class IdentifyDoctorServiceImpl implements IdentifyDoctorService {
    private static final Logger logger = LoggerFactory.getLogger(IdentifyDoctorServiceImpl.class);
    @Autowired
    private EntityManager em;

    /*
     * @Author RAICOM2023
     * @Description //根据处罚对象名称查询处罚信息
     * @Date 22:04 2023/3/6
     * @Param [penaltyInfoRequestVO]
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    @Override
    public List<Map<String, Object>> listPenaltyInfo(PenaltyInfoRequestVO penaltyInfoRequestVO) throws Exception {
        List<Map<String, Object>> penaltyInfoMapList = new ArrayList<>();
        StringBuilder sqlStr = new StringBuilder();
        sqlStr.append("select * from doctor_penalty where 1 = 1 ");

        if(StringUtils.isNotEmpty(penaltyInfoRequestVO.getYsCfdx())) {
            sqlStr.append("and ys_cfdx like '%" + penaltyInfoRequestVO.getYsCfdx().trim() + "%'");
        }
        if(StringUtils.isNotEmpty(penaltyInfoRequestVO.getYsCfzl())) {
            sqlStr.append("and ys_cfzl like '%" + penaltyInfoRequestVO.getYsCfzl().trim() + "%'");
        }
        if(StringUtils.isNotEmpty(penaltyInfoRequestVO.getYsCfzxqk())) {
            sqlStr.append("and ys_cfzxqk like '%" + penaltyInfoRequestVO.getYsCfzxqk().trim() + "%'");
        }
        String ysCfrq = penaltyInfoRequestVO.getYsCfrq();
        if(StringUtils.isNotEmpty(ysCfrq)) {
            String startDate = ysCfrq.trim().split(",")[0];
            String endDate = ysCfrq.trim().split(",")[1];
            sqlStr.append("and ys_cfrq >= '" + startDate + "' and ys_cfrq <= '" + endDate + "'");
        }
        Integer startIndex = (penaltyInfoRequestVO.getPageNo() - 1) * penaltyInfoRequestVO.getPageSize();
        //plgresql分页SQL
        /*sqlStr.append(
                " order by doctor_penalty_id asc limit '" + penaltyInfoRequestVO.getPageSize() + "' "
                        + "offset '"
                        + startIndex + "'");*/
        //MySQL分页SQL
        sqlStr.append(
                " order by ys_cfrq desc limit " + penaltyInfoRequestVO.getPageSize() + " offset " + startIndex + "");
        String sql = sqlStr.toString();

        logger.info("处罚医生查询SQL：" + sql);

        Query query = em.createNativeQuery(sql);
        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List<?> rows = query.getResultList();
        if(CollectionUtils.isEmpty(rows)){
            return penaltyInfoMapList;
        }else{
            for(Object obj : rows){
                Map<String, String> hashMap = new LinkedHashMap<>();
                Map<Object, Object> map = (Map)obj;
                for (Object key : map.keySet()) {
                    if (null == map.get(key) || "".equals(map.get(key).toString())) {
                        hashMap.put(key.toString().toUpperCase(), null);
                    } else {
                        hashMap.put(key.toString().toUpperCase(), map.get(key).toString());
                    }
                }
                Map<String, Object> penaltyInfoMap = new HashMap<>();
                penaltyInfoMap.put("id", hashMap.get("DOCTOR_PENALTY_ID"));
                penaltyInfoMap.put("ysCfdx", hashMap.get("YS_CFDX"));
                penaltyInfoMap.put("ysCfjds", hashMap.get("YS_CFJDS"));
                penaltyInfoMap.put("ysCfzl", hashMap.get("YS_CFZL"));
                penaltyInfoMap.put("ysCfsy", hashMap.get("YS_CFSY"));
                penaltyInfoMap.put("ysCfnr", hashMap.get("YS_CFNR"));
                penaltyInfoMap.put("ysCfrq", hashMap.get("YS_CFRQ"));
                penaltyInfoMap.put("ysCfzxqk", hashMap.get("YS_CFZXQK"));
                penaltyInfoMap.put("ysCfjg", hashMap.get("YS_CFJG"));
                penaltyInfoMapList.add(penaltyInfoMap);
            }
        }
        em.close();
        return penaltyInfoMapList;
    }

    /*
     * @Author RAICOM2023
     * @Description //根据处罚对象名称查询处罚信息--查询总数
     * @Date 22:14 2023/3/6
     * @Param [penaltyInfoRequestVO]
     * @return long
     **/
    @Override
    public long findPenaltyTotal(PenaltyInfoRequestVO penaltyInfoRequestVO) throws Exception {
        StringBuilder sqlStr = new StringBuilder();
        sqlStr.append("select count(*) from doctor_penalty where 1 = 1 ");
        sqlStr.append("and ys_cfdx like '%" + penaltyInfoRequestVO.getYsCfdx() + "%'");
        String sql = sqlStr.toString();
        Query query = em.createNativeQuery(sql);
        Object o = query.getSingleResult();
        em.close();
        if(o == null){
            return 0L;
        }else{
            BigInteger big = (BigInteger) o;
            return big.longValue();
        }
    }
}
